import { createRouter, createWebHistory } from "vue-router";
import { useUserStore } from "@/stores/user";
import routes from "./routes";
export * from "./utils";

const router = createRouter({
    history: createWebHistory(),
    routes: routes as any,
});

// 添加全局前置路由守卫
router.beforeEach((to, from, next) => {
    const userStore = useUserStore();
    const isLoggedIn = userStore.user.token;

    // 若目标路由不是登录页，且用户未登录，则重定向到登录页
    if (!isLoggedIn && to.name !== "login") {
        next({ name: "login" });
    } else {
        next();
    }
});

export default router;
